package com.bracketbird.server.services;


import com.bracketbird.client.model.*;
import com.bracketbird.client.service.*;
import com.bracketbird.server.*;
import com.bracketbird.server.dao.*;
import com.bracketbird.server.repository.*;
import com.bracketbird.server.repository.tournament.*;
import com.bracketbird.clientcore.appcontrol.*;
import com.bracketbird.clientcore.service.*;

/**
 *
 */
public class GetSingleAppInfoHandler extends AbstractActionHandler implements ActionHandler<GetSingleAppInfoAction, SingleResult<SingleAppInfo>> {


    public SingleResult<SingleAppInfo> execute(GetSingleAppInfoAction action) throws ApplicationException {
        SingleAppInfo sai = new SingleAppInfo();
        sai.setUser(new UserRepository().get(action.getUserId()));


        try {
            PMF.startTransaction();

            Logger.log(action.toString());
            sai.setClub(new ClubRepository().get(action.getClubId()));

            TournamentRepository tournamentRep = new TournamentRepository();
            sai.setTournament(tournamentRep.get(action.getTournamentId()));
            sai.setTournamentEventLog(tournamentRep.getTournamentLog(action.getTournamentId()));
            if (action.isFetchMembers()) {
                sai.setMembers(new MemberRepository().findByClubId(action.getClubId()));
            }

            PMF.commitTransaction();

            return new SingleResult<SingleAppInfo>(sai);
        }
        finally {
            PMF.endTransaction();
        }


    }

    public Class<GetSingleAppInfoAction> getActionType() {
        return GetSingleAppInfoAction.class;
    }


}